package android.homeaccount.helper;

import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

import android.util.Log;

public class HTTPSRequest {
	
	final int buffer_size=8 * 1024;

	public static String Request(String endpoint) {
		String data = null;
		for(int x=1;x<=2;x++)
		{
			HTTPSRequest hr = new HTTPSRequest();
			data = hr.inRequest(endpoint);
			if(data != null) break;
			else
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
				}
		}		
		return data;
	}
	
	private String inRequest(String endpoint) {		
		HttpsURLConnection connections = null;
		HttpURLConnection connection = null;
		try 
		{	
			if(endpoint.indexOf("https") >= 0)
			{				
				SSLContext context = SSLContext.getInstance("TLS");
				context.init(null, new X509TrustManager[] { new X509TrustManager() {
					public void checkClientTrusted(X509Certificate[] chain,
							String authType) throws CertificateException {
					}
	
					public void checkServerTrusted(X509Certificate[] chain,
							String authType) throws CertificateException {
					}
	
					public X509Certificate[] getAcceptedIssuers() {
						return new X509Certificate[0];
					}
				} }, new SecureRandom());
				HttpsURLConnection.setDefaultSSLSocketFactory(context
						.getSocketFactory());
				
				URL serverAddress = new URL(endpoint);
				connections = (HttpsURLConnection) serverAddress.openConnection();
				connections.connect();
				int rc = connections.getResponseCode();
				if (rc == 200) {
					String line = null;
					BufferedReader br = new BufferedReader(
							new java.io.InputStreamReader(
									connections.getInputStream(), "TIS620"),buffer_size);
					StringBuilder sb = new StringBuilder();

					while ((line = br.readLine()) != null)
						sb.append(line + '\n');

					connections.disconnect();
					return sb.toString();
				} else {
					connections.disconnect();
					//return null;
					
					context = SSLContext.getInstance("TLS");
					context.init(null, new X509TrustManager[] { new X509TrustManager() {
						public void checkClientTrusted(X509Certificate[] chain,
								String authType) throws CertificateException {
						}
		
						public void checkServerTrusted(X509Certificate[] chain,
								String authType) throws CertificateException {
						}
		
						public X509Certificate[] getAcceptedIssuers() {
							return new X509Certificate[0];
						}
					} }, new SecureRandom());
					HttpsURLConnection.setDefaultSSLSocketFactory(context
							.getSocketFactory());
					
					serverAddress = new URL(endpoint);
					connections = (HttpsURLConnection) serverAddress.openConnection();
					connections.connect();
					rc = connections.getResponseCode();
					if (rc == 200) {
						String line = null;
						BufferedReader br = new BufferedReader(
								new java.io.InputStreamReader(
										connections.getInputStream(), "TIS620"),buffer_size);
						StringBuilder sb = new StringBuilder();

						while ((line = br.readLine()) != null)
							sb.append(line + '\n');

						connections.disconnect();
						return sb.toString();
					} else {
						connections.disconnect();
						return null;
					}
				}
			}
			else{
				
				URL serverAddress = new URL(endpoint);
				connection = (HttpURLConnection) serverAddress.openConnection();
				connection.connect();
				int rc = connection.getResponseCode();
				if (rc == 200) {
					String line = null;
					BufferedReader br = new BufferedReader(
							new java.io.InputStreamReader(
									connection.getInputStream(), "TIS620"),buffer_size);
					StringBuilder sb = new StringBuilder();
	
					while ((line = br.readLine()) != null)
						sb.append(line + '\n');
	
					connection.disconnect();
					return sb.toString();
				} else {
					connection.disconnect();
					return null;
				}
			}
		} catch (Exception e) {
			try
			{
				connections.disconnect();
			}catch(Exception ex)			{}
			try
			{
				connection.disconnect();
			}catch(Exception ex)			{}
			Log.e("HTTPsRequest", e.toString());
			return null;
		}
	}
}